{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a2830ee6-0f94-4ccc-b46d-326e56d0831a",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "405a9771-3061-4c36-a8bf-b24c9116d7fa",
   "metadata": {},
   "source": [
    "## 函数的基本概念"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a64742fb-fa7f-4dbe-b1ce-36f8167ebe36",
   "metadata": {},
   "source": [
    "Python中的函数是一段可重复使用的代码块。在Python中，可以使用def关键字来定义函数。定义函数的一般形式如下："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc75fd82-1ba5-48d0-a160-b6427390d996",
   "metadata": {},
   "source": [
    "``` python\n",
    "def 函数名(参数1，参数2，...):\n",
    "    函数体\n",
    "    return 返回值\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ae74d21-42b2-4356-84eb-91737a6d0422",
   "metadata": {},
   "source": [
    "### 无参数函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a2beedba-f2a0-4128-8c98-11cd8860a43a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def greet():\n",
    "    print(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e60b99e3-08a9-4f4b-a08e-4faaaabaeaa0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "greet()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88b3a452-533c-4647-a76c-7c3f9e39e949",
   "metadata": {},
   "source": [
    "### 有参数函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3a2c382f-e574-4999-a6ae-b3ed4f4821ad",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def greet(name):\n",
    "    print(\"Hello, \" + name + \"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "feb8ae79-f11c-4e3f-9ed5-1d711c426ae1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, GG棒!\n"
     ]
    }
   ],
   "source": [
    "greet(\"GG棒\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0dab923-3d04-45b6-90e9-a45ece68efc7",
   "metadata": {},
   "source": [
    "#### 要参和无参可以同时存在，传递参数就运行有参方法，不传递参数就运行无参方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9bcbf8d4-ec8d-40fa-ba95-3ef7006860ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "greet() missing 1 required positional argument: 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[33], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m greet()\n",
      "\u001b[1;31mTypeError\u001b[0m: greet() missing 1 required positional argument: 'name'"
     ]
    }
   ],
   "source": [
    "greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "07804c1e-eb11-432f-8794-5c093faf61d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "greet()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1faa012-2b0a-4c20-a31b-55026bcc13d4",
   "metadata": {},
   "source": [
    "### 可变参数函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a7358971-a1a4-4db3-8cf7-0e0b373e0ec2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def sum_all(*args):\n",
    "    # Initialize total to 0\n",
    "    total = 0\n",
    "    # Iterate through each number in args\n",
    "    for num in args:\n",
    "        # Add the current number to the total\n",
    "        total += num\n",
    "    # Return the total sum\n",
    "    return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "2edc7db8-c6de-4836-bcb5-28c464e9420d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_all(1,2,3,4,5,6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d60678c-cb03-4e81-81de-a8c30f449ceb",
   "metadata": {},
   "source": [
    "### 关键字参数函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e0b4a015-04d1-49a7-ad0b-79cd52359082",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def print_info(name, age):\n",
    "    # Print the name\n",
    "    print(\"Name:\", name)\n",
    "    # Print the age\n",
    "    print(\"Age:\", age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "dbe132b7-5c03-4ac7-831d-04a6b596784e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: 小明\n",
      "Age: 18\n"
     ]
    }
   ],
   "source": [
    "name=\"小明\"\n",
    "age=18\n",
    "print_info(name,age)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e4459dd-f193-4cd3-ba64-d9ef868ee07b",
   "metadata": {},
   "source": [
    "### 默认参数函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "e817cf06-d7c6-4f5f-bc31-2da182a5c681",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def greet(name=\"World\"):\n",
    "    print(\"Hello, \" + name + \"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "98640e25-26d1-413c-9015-2f8d676e00b0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4ec240bd-aa39-43af-bc84-f2e67297a774",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 666!\n"
     ]
    }
   ],
   "source": [
    "greet(\"666\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bcde61f-6239-4b4c-8886-ab71a9b28909",
   "metadata": {},
   "source": [
    "### **匿名函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "93dd7f89-e46c-4590-8a0e-69ec13e36137",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "add = lambda x, y: x + y\n",
    "print(add(3, 5))  # 输出：8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b4af96ad-89aa-4f5f-a464-45aad3fd0612",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "def addsum(x,y):\n",
    "    return x+y\n",
    "print(addsum(3, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9715d88a-7e57-4734-8b78-fcfc44009f58",
   "metadata": {},
   "source": [
    "## 返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b65f165b-acc0-4bb6-a9b2-2fb50ffc6bec",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Define a function named square that takes a parameter x\n",
    "def square(x):\n",
    "    # Return the square of x\n",
    "    return x ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1a6a0516-48d3-4551-be1b-a56185bc9ccf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "sums = square(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "46ebc1c4-8f84-453d-ab60-c7a01acbb892",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sums"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1259efae-0b30-4a12-8e2f-e9d5da55e0e2",
   "metadata": {},
   "source": [
    "## 递归函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "c430bbe1-4282-4ed5-bae4-e235c2d49dc4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def factorial(n):    # 3    2     1    factorial(2) = 2 * 1   3  = 3 * factorial(2)   3 = 3*2 *1\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eda124b9-f778-4d32-a18a-c3166f333132",
   "metadata": {},
   "source": [
    "# Assistant\n",
    "The selected code defines a recursive function called `factorial` that calculates the factorial of a given number `n`. Here's a step-by-step explanation of the code:\n",
    "\n",
    "1. `def factorial(n):` - This line defines a function named `factorial` that takes one argument `n`.\n",
    "\n",
    "2. `if n == 1:` - This line checks if the value of `n` is equal to 1.\n",
    "\n",
    "3. `return 1` - If the condition is true (i.e., `n` is 1), the function returns 1, which is the base case for the factorial function.\n",
    "\n",
    "4. `else:` - If the condition in step 2 is false, the function executes the following line.\n",
    "\n",
    "5. `return n * factorial(n-1)` - This line is the recursive step. It returns the product of `n` and the result of calling the `factorial` function with the argument `n-1`. This recursive call continues until the base case is reached (i.e., `n` becomes 1), at which point the recursion stops and the final result is calculated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "1a540637-83d4-469e-8ff2-ea608143f372",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "381f6f4f-afb8-454e-addb-60ed2481b8d6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "de6733c5-2728-4613-8901-fd2de3f3c0a4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "638383e6-fda3-43c1-a804-67180c5af0dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "23684340-da3c-46a6-9c60-ccbb9b7d101b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factorial(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3dae9e8-dd97-490a-b229-7e6d1cc0d82e",
   "metadata": {},
   "source": [
    "## 文档字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "164dcabe-ac69-4e01-83cd-6f92a614763d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def greet(name):\n",
    "    \"\"\"\n",
    "    打印问候语\n",
    "    输入：name(str)\n",
    "    输出：无   没有返回值\n",
    "    \"\"\"\n",
    "    print(\"Hello, \" + name + \"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "7f13e379-56da-4c64-b8af-214a679147b5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 小明!\n"
     ]
    }
   ],
   "source": [
    "greet(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "398fa1ec-a23b-44fd-84a4-ed4c62c5c0e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "greet()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
